package com.yc.yuehui.mapper;

import com.yc.yuehui.entity.YhPhotoRace;
import com.yc.yuehui.entity.YhPhotoShow;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.yuehui.entity.YhUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author lyl
 * @since 2023-03-07
 */
public interface YhPhotoShowMapper extends BaseMapper<YhPhotoShow> {
    @Select("select * from yh_photo_show a where exists (select 1 from yh_user b where b.id = a.uid) order by votes desc ")
    @Results(
            value = {
                    @Result(column = "id", property = "id", id = true),
//                    一对多关联查询，是用主表id查询从表
//                    一对一关联查询，是用外键关联字段来查询另一个表
                    @Result(column = "uid", property = "users", one = @One(
                            select = "selectByUid"
                    ))
            }
    )
    List<YhPhotoShow> selectAll();

    @Select("select * from yh_photo_show a where exists (select 1 from yh_user b where b.id = a.uid) order by intime desc limit #{start}, #{size}")
    @Results(
            value = {
                    @Result(column = "id", property = "id", id = true),
//                    一对多关联查询，是用主表id查询从表
//                    一对一关联查询，是用外键关联字段来查询另一个表
                    @Result(column = "uid", property = "users", one = @One(
                            select = "selectByUid"
                    ))
            }
    )
    List<YhPhotoShow> selectByNewPage(@Param("start") int start, @Param("size") int size);

    @Select("select * from yh_photo_show a where exists (select 1 from yh_user b where b.id = a.uid) order by supports desc limit #{start}, #{size}")
    @Results(
            value = {
                    @Result(column = "id", property = "id", id = true),
//                    一对多关联查询，是用主表id查询从表
//                    一对一关联查询，是用外键关联字段来查询另一个表
                    @Result(column = "uid", property = "users", one = @One(
                            select = "selectByUid"
                    ))
            }
    )
    List<YhPhotoShow> selectByHotPage(@Param("start") int start, @Param("size") int size);

    @Select("select * from yh_photo_show where id = #{id}")
    @Results(
            value = {
                    @Result(column = "id", property = "id", id = true),
//                    一对多关联查询，是用主表id查询从表
//                    一对一关联查询，是用外键关联字段来查询另一个表
                    @Result(column = "uid", property = "users", one = @One(
                            select = "selectByUid"
                    )),
                    @Result(column = "psid", property = "race", one = @One(
                            select = "selectByRid"
                    ))
            }
    )
    List<YhPhotoShow> query(int id);



    @Select("select count(*) from yh_photo_show a where exists (select 1 from yh_user b where b.id = a.uid)")
    long countPage();

    @Select("select * from yh_user where id = #{uid}")
    YhUser selectByUid(int uid);

    @Select("select * from yh_photo_race where id = #{psid}")
    YhPhotoRace selectByRid(int psid);
}
